package com.doumi.biz.qadev.api.testcase.commonfilter;

import com.doumi.biz.qadev.api.methods.OperationRedis;
import com.doumi.biz.qadev.api.testcase.RequestTemplate;
import io.restassured.path.json.JsonPath;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * Created by YDZ on 2019/11/20.
 */
public class CommonFilterName {
    private RequestTemplate requestTemplate = new RequestTemplate();
    private OperationRedis operationRedis = new OperationRedis();
    private static List<String> cuIds = new ArrayList<>();

    //redis初始化数据
    @BeforeClass(description = "初始化redis数据")
    public  void operationData(){
        String filenanme = "src/source/redisfile/filter-username.csv";
        String mothodname = "username";
        cuIds = operationRedis.readCsvoperationRedis(filenanme,mothodname,25);
    }
    @Test(description = "姓名包含敏感词、违禁词共有26个用户，其中526381793、526381794为正常用户，526381795年龄不符合，其他为姓名包含敏感词；校验传入的结果中是否只返回526381793、526381794，code状态码是否为0，msg信息是否为success")
    public void checkCusrIdNameContactSensitiveWord(){
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i <cuIds.size()-1;  i++) {
            sb.append(cuIds.get(i).trim()).append(",");
        }
        sb.append(cuIds.get(cuIds.size()-1).trim());
        sb.append("]");
        HashMap<String,Object> hashMap = new HashMap<String,Object>();
        hashMap.put("cuIds",sb.toString());
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String msg = jsonPath.getString("msg");
        //校验每个参数
        List<String> diffUserid = new ArrayList<String>(cuIds);
        for (String userid: cuIds ) {
            for (Object aResurlt : resurlt) {
                if (Objects.equals(String.valueOf(aResurlt),String.valueOf(userid))) {
                    diffUserid.remove(userid);
                    break;
                }
            }
        }
        Reporter.log("有被过滤掉的数据，具体的userid:"+diffUserid);
        Assert.assertEquals(21,diffUserid.size());
        Assert.assertEquals(5,resurlt.size());
        Assert.assertEquals(msg,"success");
    }

    @Test(description = "姓名敏感词中只有一个用户526381780，并且该用户信命中包含敏感词，预期返回结果为空")
    public void checkOnlyoneCusrIdNameContactSensitiveWord(){
        HashMap<String,Object> hashMap = new HashMap<String,Object>();
        hashMap.put("cuIds","526381780");
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),0);
        Assert.assertEquals(code,"200");
    }
    @Test(description = "姓名敏感词校验，其中两个用户509994108,509994109都命中铭感次，预期返回结果为空")
    public void checkTwoCusrIdNameContactSensitiveWord(){
        HashMap<String,Object> hashMap = new HashMap<String,Object>();
        String cuIds="[509994108,509994109]";
        hashMap.put("cuIds",cuIds);
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),0);
        Assert.assertEquals(code,"200");
    }
    @Test(description = "姓名敏感词校验，509994106包含违禁词，509994102为正常用户，预期返回526381799")
    public void checkCusrIdNameContactBannedWord(){
        HashMap<String,Object> hashMap = new HashMap<String,Object>();
        String cuIds="[509994106,509994102]";
        hashMap.put("cuIds",cuIds);
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),1);
        Assert.assertEquals(String.valueOf(resurlt.get(0)),String.valueOf(509994102));
        Assert.assertEquals(code,"200");
    }
    //删除数据
    @AfterClass(description = "删除出入掉的数据，避免影响其他测试")
    public void removeData(){
        operationRedis.removeRedis(cuIds);
    }
}
